#!/usr/bin/env python
#coding: utf-8
#
import json
from redishelper import RedisHelper
class BaseMonitor:
	def __init__(self,name,interval,plginname,lasttime,threshold):
		self.name = name
		self.interval = interval
		self.plginname = plginname
		self.lasttime = lasttime
		self.threshold = threshold


class DefaultLoadMonitor(BaseMonitor):
	def __init__(self):
		name = 'cpu'
		interval = 7
		plginname = 'get_cpu_info'
		lasttime = 0
		threshold = {
			'load1':[0.01,0.1],
			'load5':[0.05,0.2],
			'load15':[0.5,0.1]

		} 

class DefaultCpuMonitor(BaseMonitor):
	def __init__(self):
		name = 'cpu'
		interval = 7
		plginname = 'get_cpu_info'
		lasttime = 0
		threshold = {
			'load1':[0.01,0.1],
			'load5':[0.05,0.2],
			'load15':[0.5,0.1]

		} 

class DefaultMemMonitor(BaseMonitor):
	def __init__(self):
		name = 'cpu'
		interval = 7
		plginname = 'get_cpu_info'
		lasttime = 0
		threshold = {
			'load1':[0.01,0.1],
			'load5':[0.05,0.2],
			'load15':[0.5,0.1]

		} 
		BaseMonitor.__init__(self, name, interval, plginname, lasttime, threshold)
class BaseTemplate:
	def __init__(self,cpu,load,memory):
		self.__services = {
			'cpu':cpu,
			'load':load,
			'memory':memory,
		}

	@property
	def editcpu(self):
	    return self.___services['cpu']
	@property
	def editload(self):
	    return self.___services['load']
	
	@property
	def editmemory(self):
	    return self.__services['memory ']

def init_template():
	t1 = BaseTemplate(cpu=DefaultCpuMonitor(),load=DefaultLoadMonitor(), memory=DefaultMemMonitor())
	return {'template_1':t1}

def push_config_to_redis(rediscli,templates):
	for key,template in templates.item():
		config = {}
		
		for k,v in template.threshold.items():
			config[key] = {'interval':templates.interval,'plginname':templates.plginname,'lasttime':0}
			for key,value in v.threshold.items():
				config[k][key] = value
		rediscli.set(key,json.dumps(config))
def init_hostname_certname():
	hosts = {
		'linuxchobit.pppp.com':'template_1'

	}
	for key,value in hosts.items():
		rediscli.set(key,value)

def run(result):
	while True:
		data = result.parse_response()